﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Web.Models;
using DataLibrary;
using DataLibrary.Entity;
using Microsoft.EntityFrameworkCore;

namespace Web.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MagicController : ControllerBase
    {
        private protected UpgradeDbContext DbContext { get; set; }
        public MagicController(UpgradeDbContext dbContext)
        {
            DbContext = dbContext;
        }
        [HttpPost]
        public async Task<OperationResult> PostAsync([FromBody] AddMagicModel model)
        {
            OperationResult result = new OperationResult();

            var account = DbContext.Accounts.FirstOrDefault(x => x.User == User.Identity.Name);
            if (account != null)
            {
                await DbContext.Products.AddAsync(new Product()
                {
                    Id = Guid.NewGuid().ToString().Replace("-", ""),
                    Name = model.Name,
                    Remarks = model.Remarks,
                    AccountId = account.Id
                });
                if (await DbContext.SaveChangesAsync() > 0)
                    result.Ok = true;
                else
                    result.Mess = "添加失败";
            }
            else
            {
                result.Mess = "添加失败,请联系管理员!";
            }
            return result;
        }
        [HttpPatch]
        public async Task<OperationResult> PatchAsync([FromBody]UpdataMagicModel model)
        {
            OperationResult result = new OperationResult();

            var magic = DbContext.Products.FirstOrDefault(x => x.Id == model.Id);
            if (magic != null)
            {
                magic.UpExplain = model.Explain;
                if (await DbContext.SaveChangesAsync() > 0)
                    result.Ok = true;
                else
                    result.Mess = "修改失败!";
            }
            else
            {
                result.Mess = "项目不存在";
            }

            return result;
        }
        [HttpPost("FindList")]
        public FindListResult FindList([FromBody]WhereMagicModel model)
        {
            FindListResult result = new FindListResult();
            result.Data = (from p in DbContext.Products
                           join u in DbContext.Accounts
                           on p.AccountId equals u.Id
                           where u.User == User.Identity.Name
                           select p)
                           .AsNoTracking()
                           .Skip(model.Limit * (model.Page - 1))
                           .Take(model.Limit)
                           .ToList();

            result.Count = (from p in DbContext.Products
                            join u in DbContext.Accounts
                            on p.AccountId equals u.Id
                            where u.User == User.Identity.Name
                            select p)
                            .Count();

            return result;
        }
    }
}